<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Include the {@link shared.make_timestamp.php} plugin
 */
require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
/**
 * Smarty date_format modifier plugin
 *
 * Type:     modifier<br>
 * Name:     time_ago<br>
 * Purpose:  format datestamps via strftime<br>
 * Input:<br>
 *         - string: input date string
 *         - format: strftime format for output
 *         - default_date: default date if $string is empty
 * @link http://smarty.php.net/manual/en/language.modifier.date.format.php
 *          date_format (Smarty online manual)
 * @author   Monte Ohrt <bhatupawar85 at gmail dot com>
 * @param string
 * @param string
 * @param string
 * @return string|void
 * @uses smarty_make_timestamp()
 */
function smarty_modifier_time_ago($date_string)
{
		$c = getdate();
		$p = array('year', 'mon', 'mday', 'hours', 'minutes', 'seconds');
		$display = array('year', 'month', 'day', 'hour', 'minute', 'second');
		$factor = array(0, 12, 30, 24, 60, 60);
		$d = datetoarr($date_string);

		for ($w = 0; $w < 6; $w++) {
			if ($w > 0) {
				$c[$p[$w]] += $c[$p[$w-1]] * $factor[$w];
				$d[$p[$w]] += $d[$p[$w-1]] * $factor[$w];
			}
			if ($c[$p[$w]] - $d[$p[$w]] > 1) { 
				return ($c[$p[$w]] - $d[$p[$w]]).' '.$display[$w].'s ago';
			}
		}
		return '';
}

/* vim: set expandtab: */

?>
